<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <BaseDialog
      :visible.sync="isOwner"
      :title="'选择经销商'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <ownerSeacher  @close="ownercloseHandler" />
    </BaseDialog>
    <BaseDialog
      :visible.sync="isSelectCar"
      :title="'车辆信息'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <vinSelect  @close="carcloseHandler" />
    </BaseDialog>
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">提 交</multiuse-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 3)">保 存</multiuse-button>
      <!-- <el-button @click="closeDrawer(2)" type="primary">提 交</el-button>
      <el-button @click="closeDrawer(2)" type="primary">保 存</el-button> -->
    </div>
  </div>
</template>
<script>
import {getList,vehicleConfiguration,insertAllocateApply,} from '@/api/dealerSale/TransferManager/Transferred/index.js'
import ownerSeacher from './ownerSeacher';
import { dictionary } from '@/utils/dictMixin';
import vinSelect from './vinSelect';
import moment from 'moment';
import cookie from 'js-cookie'
export default {
  data(){
    this.saveData=null;
    return{
      formList:this.createdFrom(),
      isOwner:false,
      isSelectCar:false,
      OwnerData:{},
      ownerCode:'',
      vehicleId:'',
      username:cookie.get('username'),
      dealerCode:cookie.get('ownerCode')
    }
  },
  components: {
    ownerSeacher,
    vinSelect
  },
  created(){
    this.formList.map(x=>{
      if(x.fieldName=='storagePersonName'){
        x.initialValue=this.username;
      }
    })
  },
  mixins:[dictionary],
  methods:{
    createdFrom(){
      return [
        {
          type: 'INPUT',
          label: '调拨单号',
          fieldName: 'dbdh',
          placeholder: '请输入',
          disabled:true,
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          readonly:true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          },
          rules: [{ required: true, message: '请选择', trigger: 'blur' }],
        },

        {
          type: 'INPUT',
          label: '调入方',
          fieldName: 'toDealerName',
          placeholder: '请输入',
          readonly:true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(2)}></el-button>
              </div>
            );
          },
          rules: [{ required: true, message: '请选择', trigger: 'blur' }],
        },
        {
          type: 'INPUT',
          label: '调拨人',
          fieldName: 'applyPersonName',
          placeholder: '请输入',
          disabled:true,
        },
        {
          type: 'DATE',
          label: '调拨申请时间',
          fieldName: 'dbsqsj',
          placeholder: '请选择',
          disabled:true,
          initialValue:moment().format('YYYY-MM-DD hh:mm:ss')
        },
        {
          type: 'TEXT_AREA',
          label: '调出说明',
          fieldName: 'applyRemark',
          placeholder: '请输入',
          selfCols:3
        },

      ]
    },
    carcloseHandler(val){
      if(val){
        console.log(val)
        this.formList.map(x=>{
          if(x.fieldName=='vin'){
            x.initialValue=val.vin;
            this.vehicleId=val.vehicleId
          }
        })
      }
      this.isSelectCar=false;
    },
    ownercloseHandler(val){
      if(val){
        console.log(val)
        this.formList.map(x=>{
          if(x.fieldName=='toDealerName'){
            x.initialValue=val.dealerName;
            this.ownerCode=val.dealerCode
          }
        })
      }
      this.isOwner=false;
    },

    seacherFun(type){
      if(type==1){
        this.isSelectCar=true;
      }else{
        this.isOwner=true;
      }
    },
    changeHandle(val){
      this.saveData=val;
    },
    async closeDrawer(type){
      if(type==1){
        this.$emit('close')
      }else if(type==2){

        this.$refs.from.SUBMIT_FORM();
        if (this.saveData ) {
          let json=JSON.parse(JSON.stringify(this.saveData))
          if(this.dealerCode==this.ownerCode){
            this.$notify({
              title: '警告',
              message: '调出方不能与调入方相同',
              type: 'warning'
            });
            return
          }
          json.toDealer=this.ownerCode;
          json.vehicleId=this.vehicleId
          json.applyStatus=60121002
          let res=await insertAllocateApply(json)
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '提交成功',
              type: 'success'
            });
            this.$emit('close', json);
          }else{
            this.saveData=null;
          }
        }
      }else{
        this.$refs.from.SUBMIT_FORM();
        if (this.saveData ) {
          let json=JSON.parse(JSON.stringify(this.saveData))
          if(this.dealerCode==this.ownerCode){
            this.$notify({
              title: '警告',
              message: '调出方不能与调入方相同',
              type: 'warning'
            });
            return
          }
          json.toDealer=this.ownerCode;
          json.vehicleId=this.vehicleId;
          json.applyStatus=60121001
          let res=await insertAllocateApply(json)
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '保存成功',
              type: 'success'
            });
            this.$emit('close', json);
          }else{
            this.saveData=null;
          }
        }
      }
    }

  }
}
</script>
